package com.yunshu.yunshuapp.kuaixun;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.yunshu.yunshuapp.OnTapListener;
import com.yunshu.yunshuapp.OnTapListener2;
import com.yunshu.yunshuapp.banner.ScrollBanner;
import com.yunshu.yunshuapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouwenjie on 2017/1/15.
 */
public class TypeListAdapter extends RecyclerView.Adapter {


    private ArrayList<KuaixunChannel> models;
    public OnTapListener tapListener;
    public OnTapListener2 tapListener2;
    public Activity activity;
    public boolean allowMultiSelect = false;

    public TypeListAdapter(Context context){

        models = new ArrayList<>();

    }

    @Override
    public int getItemCount() {

        int count = models.size();
        return count ;
    }

    @Override
    public int getItemViewType(int position) {

        return 0;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.subscribe_list_item,parent,false);
        ListViewHolder holder = new ListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        KuaixunChannel model = models.get(position);
        ((ListViewHolder)holder).setPosition(position);
        ((ListViewHolder)holder).adapter = this;
        ListViewHolder listHolder = (ListViewHolder) holder;
        listHolder.titleTV.setText(model.getTitle());
        listHolder.checkBtn.setChecked( model.isSelected );

    }


    public void addFooterItem(List<KuaixunChannel> footerDatas) {
        models.addAll(footerDatas);
    }

    class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView titleTV;
        CheckBox checkBtn;
        int mPosition;
        TypeListAdapter adapter;

        public ListViewHolder(View itemView ) {
            super(itemView);
            itemView.setOnClickListener(this);
            checkBtn = (CheckBox) itemView.findViewById(R.id.checkBtn);
            titleTV = (TextView) itemView.findViewById(R.id.textView1);
            checkBtn.setOnClickListener(this);
        }

        public void setPosition(int position) {
            mPosition = position;
        }


        public void onClick(View v) {
            CheckBox checkBtn1 = null;
            if (v == checkBtn){
                checkBtn1 = (CheckBox) v;
            }else{
                checkBtn1 = (CheckBox) v.findViewById(R.id.checkBtn);
                if (!allowMultiSelect) {
                    checkBtn1.setChecked(true);
                }else{
                    checkBtn1.setChecked(!checkBtn1.isChecked());
                }
            }

            int i = 0;
            for (KuaixunChannel iModel : adapter.getModels()){
                if (!allowMultiSelect) {
                    if (i == mPosition) iModel.isSelected = true;
                    else iModel.isSelected = false;
                }else{
                    if (i == mPosition ) iModel.isSelected = !iModel.isSelected;

                }
                i ++ ;
            }

            if (tapListener != null) {
                tapListener.onSingleTap(checkBtn1, mPosition);
            }
            if (tapListener2 != null) {
                tapListener2.onSingleTap(adapter , checkBtn1, mPosition);
            }

        }
    }

    public void setModels(List<KuaixunChannel> newModels){
        models.clear();
        if (newModels == null) return;
        models.addAll(newModels);
    }

    public List<KuaixunChannel> getModels(){
        return models;
    }

}
